package Service;

import java.util.ArrayList;
import java.util.List;

import Dao.WeiboFriend;
import Data.UserFriend;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class WeiboFun implements Response{

    private StringBuffer jsonString ; 
    
    private String userId ; 
    
    private String method ;
    
    public WeiboFun( String userId , String method ){
	this.userId = userId ;
	this.method = method ;
    }
    
    
    @Override
    public void parse() {
	// TODO Auto-generated method stub
	switch (method) {
	case "friend":
	    parseFriend(); 
	    break;

	default:
	    break;
	}
    }

    private void parseFriend(){
	
	JSONObject jsonObject = JSONObject.fromObject(jsonString.toString()) ;
	
	JSONArray jsonArray = jsonObject.getJSONArray("users") ;
	
	WeiboFriend weiboFriend = new WeiboFriend() ;
	
	weiboFriend.deleteFriends(userId);
	
	for( int i = 0 ; i <jsonArray.size() ; i++){
	    
	    JSONObject userJsonObject = jsonArray.getJSONObject(i) ;
	    
	    UserFriend userFriend = new UserFriend() ;
	    
	    userFriend.setUserId(userId);
	    
	    userFriend.setFriendId(userJsonObject.getString("idstr"));
	    
	    userFriend.setFriendName(userJsonObject.getString("name"));
	    
	    weiboFriend.insetFriends(userFriend);
	    
	}
	
    }


    @Override
    public void setJsonString(StringBuffer jsonString) {
	// TODO Auto-generated method stub
	this.jsonString = jsonString ;
    }
    
    
}
